/**
 * 
 */
package com.wubo.bm.common.action;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.wubo.bm.common.constant.ResponseType;
import com.wubo.bm.common.entity.FeedBack;
import com.wubo.bm.common.service.IFeedBackService;
import com.wubo.bm.exception.BackendException;
import com.wubo.common.util.Assert;
import com.wubo.dao.PageBean;
import com.wubo.sec.SecurityManager;
import com.wubo.sec.core.HttpServletRequestHolder;
import com.wubo.sec.core.UserData;
import com.wubo.sec.model.User;
import com.wubo.sec.web.action.BaseSecAction;

/**
 * @author WuBo
 * @CreateDate 2012-2-22 下午03:09:31
 */
@Controller
public class FeedBackAction extends BaseSecAction {
	@Resource
	private IFeedBackService feedBackService;
	
	private FeedBack feedBack;
	private Integer id;
	private int responseType; //回复类型
	private PageBean<FeedBack> pageBean;
	
	public String enter(){
		super.setUserAttributesToRequest();
		return SUCCESS;
	}
	
	public String list(){
		super.setUserAttributesToRequest();
		
		responseType = ResponseType.FEEDBACK;
		
		pageBean = feedBackService.xqueryForPageBean(start, limit, new String[]{"audited="}, new String[]{"emitDate@A"}, true);
		return "list";
	}
	
	public void emit(){
		try{
			Assert.notNull(feedBack);
			Assert.hasText(feedBack.getContent());
			Assert.hasText(feedBack.getTitle());
			UserData user = SecurityManager.getCurrentUser();
			Assert.notNull(user);
			
			feedBack.setEmitDate(new Date(System.currentTimeMillis()));
			feedBack.setAudited(true);
			feedBack.setIp(HttpServletRequestHolder.getIP());
			feedBack.setUser((User)user);
			feedBackService.save(feedBack);
			jsonSuccess(null);
		}catch(RuntimeException e){
			jsonError(null);
			throw e;
		}
	}

	public void del(){
		try{
			if(id > 0){
				feedBackService.delete(feedBackService.get(id));
				jsonSuccess(null);
			}
		}catch(BackendException e){
			jsonError(e.getMessage());
		}catch(RuntimeException e){
			jsonError(null);
			throw e;
		}
	}
	
	public FeedBack getFeedBack() {
		return feedBack;
	}
	public void setFeedBack(FeedBack feedBack) {
		this.feedBack = feedBack;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public PageBean<FeedBack> getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean<FeedBack> pageBean) {
		this.pageBean = pageBean;
	}
	public int getResponseType() {
		return responseType;
	}
	public void setResponseType(int responseType) {
		this.responseType = responseType;
	}
	
}
